package nl.fotoniq.android.lockpicker.shared;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

/**
 * The Class LockPickerUtil.
 * <p>
 * This is a utility class.
 * </p>
 * 
 * @author jeroen.brosens
 */
public final class LockPickerUtil {

	/**
	 * Instantiates a new LockPickerUtil.
	 */
	private LockPickerUtil() {
	}

	/**
	 * Log at the DEBUG level.
	 * 
	 * @param msg
	 *            the message
	 */
	public static void log_d(final String msg) {
		Log.d(LockPickerConstants.TAG, msg);
	}

	/**
	 * Log at the ERROR level.
	 * 
	 * @param msg
	 *            the message
	 * @param t
	 *            the throwable
	 */
	public static void log_e(final String msg, final Throwable t) {
		if (t != null) {
			Log.e(LockPickerConstants.TAG, msg, t);
		} else {
			Log.e(LockPickerConstants.TAG, msg);
		}
	}

	/**
	 * Checks if lock setting is present.
	 * 
	 * @param context
	 *            the context
	 * @return true, if lock setting is present
	 */
	public static boolean isLockSettingPresent(final Context context) {
		String lockSetting = Settings.System.getString(context.getContentResolver(),
				LockPickerConstants.LOCK_SETTING_NAME);

		boolean result = (lockSetting != null);

		log_d("Lockscreen setting present: " + result);

		return result;
	}

	/**
	 * Checks if the service is expected to start.
	 * 
	 * @param context
	 *            the context
	 * @return true, if the service is expected to start
	 */
	public static boolean isServiceExpectedToStart(final Context context) {
		SharedPreferences preferences = context.getSharedPreferences(LockPickerConstants.PREFERENCES_NAME,
				Context.MODE_PRIVATE);

		return preferences.getBoolean(context.getString(R.string.pref_service_key),
				LockPickerConstants.DEFAULT_SERVICE_START_ON_BOOT);
	}

	/**
	 * Gets the application version name.
	 * 
	 * @param context
	 *            the context
	 * @return the application version name
	 */
	public static String getApplicationVersionName(final Context context) {
		String result = null;
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
					PackageManager.GET_ACTIVITIES);

			if (info != null) {
				result = info.versionName;
			}
		} catch (NameNotFoundException e) {
			log_e("Could not retrieve version name", e);
		}
		return result;
	}

	/**
	 * Checks if is service running.
	 * 
	 * @param context
	 *            the context
	 * @return true, if is service running
	 */
	public static boolean isServiceRunning(final Context context) {
		try {
			ActivityManager activityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);

			List<ActivityManager.RunningServiceInfo> runningServices = activityManager
					.getRunningServices(activityManager.getRunningAppProcesses().size());

			for (ActivityManager.RunningServiceInfo runningServiceInfo : runningServices) {
				if (runningServiceInfo.service.getClassName().equals(LockPickerService.class.getName())) {
					log_d("Service running");

					return true;
				}
			}
		} catch (SecurityException e) {
			log_e("Checking running services failed", e);
		}
		log_d("Service not running");

		return false;
	}

	/**
	 * Gets the application main activity class.
	 * 
	 * @param context
	 *            the context
	 * @return the application main activity class
	 */
	public static Class<?> getApplicationMainActivityClass(final Context context) {
		Class<?> clazz = null;
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
					PackageManager.GET_ACTIVITIES);

			if (info != null && info.activities.length > 0) {
				clazz = Class.forName(info.activities[0].name);

				log_d("Main activity class: " + clazz.getName());
			}
		} catch (NameNotFoundException e) {
			log_e("Could not retrieve version name", e);
		} catch (ClassNotFoundException e) {
			log_e("Could not retrieve main activity class", e);
		}
		return clazz;
	}

	/**
	 * Blur and dim background.
	 * 
	 * @param dialog
	 *            the dialog
	 */
	public static void blurAndDimBackground(final Dialog dialog) {
		// Disable background dimming
		WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
		layoutParams.dimAmount = 0.0f;
		dialog.getWindow().setAttributes(layoutParams);

		// Blur background
		dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
	}

	/**
	 * Send email.
	 * 
	 * @param context
	 *            the context
	 */
	public static void sendEmail(final Context context) {
		final Intent i = new Intent(android.content.Intent.ACTION_SEND);
		i.setType("plain/text");
		i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "j.brosens@gmail.com" });
		i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Inquiry from LockPicker app");
		i.putExtra(android.content.Intent.EXTRA_TEXT, "");

		context.startActivity(Intent.createChooser(i, "Send mail..."));
	}
}
